package l1j.william;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.utils.SQLUtil;

public class Reward {

	private static final Logger _log = Logger.getLogger(Reward.class.getName());
	private static Reward _instance;

	public static Reward getInstance() {
		if (_instance == null) {
			_instance = new Reward();
		}
		return _instance;
	}

	private final HashMap<Integer, RewardModel> _rewardList = new HashMap<Integer, RewardModel>();

	private static final String TOKEN = ",";

	public Reward() {
		this.LoadData();
	}

	private int[] getArray(final String s) {
		try {
			final StringTokenizer st = new StringTokenizer(s, TOKEN);
			final int iSize = st.countTokens();
			String sTemp = null;
			final int[] iReturn = new int[iSize];
			for (int i = 0; i < iSize; i++) {
				sTemp = st.nextToken();
				iReturn[i] = Integer.parseInt(sTemp);
			}
			return iReturn;
		} catch (final Exception ex) {
			_log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
		}
		return null;
	}
	/*
	 * private static Object getArray(final String s ,final int iType) { final
	 * StringTokenizer st = new StringTokenizer(s, TOKEN); final int iSize =
	 * st.countTokens(); String sTemp = null; if (iType == 1) { // int final
	 * int[] iReturn = new int[iSize]; for (int i = 0; i < iSize; i++) { sTemp =
	 * st.nextToken(); iReturn[i] = Integer.parseInt(sTemp); } return iReturn; }
	 * if (iType == 2) { // String final String[] sReturn = new String[iSize];
	 * for (int i = 0; i < iSize; i++) { sTemp = st.nextToken(); sReturn[i] =
	 * sTemp; } return sReturn; } if (iType == 3) { // String String sReturn =
	 * null; for (int i = 0; i < iSize; i++) { sTemp = st.nextToken(); sReturn =
	 * sTemp; } return sReturn; } return null; }
	 */

	public void getItem(final L1PcInstance pc) {
		final int lv = pc.getLevel();
		final RewardModel model = this._rewardList.get(lv);
		if (model != null) {
			model.getItemList(pc);
		}
	}

	private void LoadData() {
		Connection con = null;
		Statement stat = null;
		ResultSet rset = null;

		try {
			con = DatabaseFactory.get().getConnection();
			stat = con.createStatement();
			rset = stat.executeQuery("SELECT * FROM william_reward");

			while (rset.next()) {
				final int lv = rset.getInt("level");
				RewardModel model = this._rewardList.get(lv);
				final int[] itemid = this.getArray(rset.getString("getItem"));
				final int[] count = this.getArray(rset.getString("count"));
				final int[] enchant = this.getArray(rset.getString("enchantlvl"));
				if (model == null) {
					model = new RewardModel();
					model.setMsg(rset.getString("message"));
					model.setQuestId(rset.getInt("quest_id"));
					model.setQuestStep(rset.getInt("quest_step"));
					this._rewardList.put(lv, model);
				}
				if (rset.getInt("give_royal") == 1) {
					model.addItemList(0, itemid, count, enchant);
				}
				if (rset.getInt("give_knight") == 1) {
					model.addItemList(1, itemid, count, enchant);
				}
				if (rset.getInt("give_elf") == 1) {
					model.addItemList(2, itemid, count, enchant);
				}
				if (rset.getInt("give_mage") == 1) {
					model.addItemList(3, itemid, count, enchant);
				}
				if (rset.getInt("give_darkelf") == 1) {
					model.addItemList(4, itemid, count, enchant);
				}
				if (rset.getInt("give_DragonKnight") == 1) {
					model.addItemList(5, itemid, count, enchant);
				}
				if (rset.getInt("give_Illusionist") == 1) {
					model.addItemList(6, itemid, count, enchant);
				}
			}
		} catch (final Exception ex) {
			_log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
		} finally {
			SQLUtil.close(rset, stat, con);
		}
	}
}
